<?php
require_once('inc/init.php');
require_once getCacheFilePath('forums.php');

//是否论坛开放注册
if($cache_settings['issignup']=='0'){
	printMsg('signup_signupoff');
}
$headtitle = "会员注册";
//同一IP注册间隔时间
$signupip=getIP();
$limittime = $_SYS['time']-$cache_settings['signupitime']*3600;
$signup_cond="signupip='{$signupip}' and signuptime>{$limittime}";
$row = $db->row_select_one("users", $signup_cond,"id");
if(!empty($row)){
	printMsg('signup_signupitime');
}

$action=strFilter($_GET['action']);
$username=strFilter($_POST['username']);
$userpass=strFilter($_POST['userpass']);
$repass=strFilter($_POST['repass']);
$email=strFilter($_POST['email']);
$signupreason=strFilter($_POST['signupreason']);
$securitycode=strFilter($_POST['securitycode']);
$formaction="signup.php?action=save";
if($action=="save"){
	if($username==""||$userpass==""||$email==""){
		printMsg('signup_required');
	}
	//用户名长度
	$userlen=strlen($username);
	if($userlen<4||$userlen>24){
		printMsg('signup_userlength');
	}
	//用户名是否合法
	if(!isValidName($username)){
		printMsg('signup_usernotvalid');
	}
	//是否有禁用关键字
	foreach(explode(",", $cache_settings['reservedkeyword']) as $rkey){
		if(!empty($rkey) && stristr($username,$rkey)){
			printMsg('signup_reservedkeyword');
		}
	}
	
	//用户名是否已经被使用
	$row=$db->row_select_one("users","username='{$username}'","id");
	if($row!=null){
		printMsg('signup_userexist');
	}

	//密码长度
	$passlen=strlen($userpass);
	if($passlen<6||$passlen>16){
		printMsg('signup_passlength');
	}
	//检测重复密码
	if($userpass!=$repass){
		printMsg('signup_repasserr');		
	}
	//是否合法的邮箱
	if(!isValidEmail($email)){
		printMsg('signup_emailnotvalid');
	}
	//邮箱是否已经被使用
	$row=$db->row_select_one("users","email='{$email}'","id");
	if($row!=null){
		printMsg('signup_emailexist');
	}
	//验证码
	if($cache_settings['signupsecuritycode']=='1' && strtolower($_SESSION['validationcode'])!=strtolower($securitycode)){
		printMsg('signup_validationcodeerr');
	}
	
	$user['username'] = $username;
	$user['userpass'] = encrypt($username,$userpass);
	$user['email'] = $email;
	$user['signuptime'] = $_SYS['time'];
	$user['signupip'] = $signupip;
	$user['groupid'] = 1;	//默认为1，普通会员
	if($cache_settings['issignupverify']=='1' || $cache_settings['issignupverify']=='2'){
		$user['groupid'] = GROUP_NOVERIFY;	// 等待验证会员
		$user['signupreason'] = $signupreason;
	}
	$db->row_insert("users",$user);
	$newuserid=$db->insert_id();
	$db->query_unbuffered("update `{$db->pre}cache1` set usersnum=usersnum+1,lastuser='{$username}'");


	//发送欢迎短信
	if($cache_settings['iswelcome']=='1'){
			$msgarr = str_replace(array('{username}', '{time}', '{bbsname}', '{url}'), array($username, getDateStr($_SYS['time'],false,false), $cache_settings['bbsname'],  $cache_settings['url']), array( $cache_settings['welcometitle'], $cache_settings['welcomecontent'] ));
			$msg['title']=$msgarr[0];
			$msg['content']=$msgarr[1];
			$msg['touserid']=$newuserid;
			$msg['fromuserid']=0;
			$msg['posttime']= $_SYS['time'];
			$db->row_insert("msgs",$msg);
	}
	
	if($cache_settings['issignupverify']=='1'){
		printMsg('signup_succeed_1');
	}
	elseif($cache_settings['issignupverify']=='2'){
		//邮箱激活
		$activecode=md5($username.$user['userpass'].$_SYS['time'].mt_rand(1000,9999));
		$userfield['userid'] = $newuserid;
		$userfield['code'] = $activecode;
		$userfield['createtime'] = $_SYS['time'];
		$userfield['type'] = 0;	//用户激活
		$db->row_insert("userfield",$userfield);
		$row=$user;
		$url=getUrlPath()."/public.php?action=active&uid={$newuserid}&code={$activecode}";
		$subject="用户激活邮件——{$cache_settings['bbsname']}";
		$body="尊敬的{$row['username']},<br />您好！这是由<a href=\"{$cache_settings['url']}\" target=\"_blank\">{$cache_settings['bbsname']}</a>发出的用户激活邮件，请点击下面的链接激活您的帐号。<br /><a href=\"{$url}\" target=\"_blank\">{$url}</a>";
		require_once('inc/email.php');
		if(sendMail($cache_settings['mailsendtype'], $row['email'], "", $subject, $body)){
			//echo("y");
		}else{
			//echo("邮件发送失败，请联系管理员。");
		}
		printMsg('signup_succeed_2');
	}
	else{
		printMsg('signup_succeed_0');
	}
	
}else{
	require_once('header.php');
	require_once getTemplatePath('signup.htm');
	footer();
}
?>